<?php
/**
 * 达人秀
 * User: lz
 * Date: 2016/1/12
 * Time: 16:58
 */
class ShowApp extends MallbaseApp {

    private $common;

    public function __construct() {
        $this->common = new CommonApp();
    }
    /**
     * APP首页达人秀列表
     * @author lz
     * @date 2016-2-15
     */
    public function index(){
        $action = 'APP首页达人秀列表';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $page = $this->get_gp('page', 'G');
        $size = $this->get_gp('size', 'G');
        $page = $page ? intval($page) : 1;
        $size = $size ? intval($size) : 10;
        $user_id = $this->checkLogin();
        $offset = ($page > 1) ? ($page - 1) * $size : 0;
        //获取达人秀列表
        $model = m('show');
        $common = $this->common;
        $list = $model->getList($offset,$size,$common,$user_id);
        if($list) {
            $this->ajax_return(FLAG_OK,'',$list);
        } else {
            $this->ajax_return(FLAG_ERROR,'暂无内容');
        }
    }

    /**
     * APP首页达人秀列表
     * @author lz
     * @date 2016-2-15
     */
    public function topic(){
        $action = 'APP首页话题列表';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $page = $this->get_gp('page', 'G');
        $size = $this->get_gp('size', 'G');
        $page = $page ? intval($page) : 1;
        $size = $size ? intval($size) : 10;
        $offset = ($page > 1) ? ($page - 1) * $size : 0;
        //获取达人秀列表
        $model = m('show');
        $topic = $model->getTopicList($offset,$size);
        if($topic) {
            $this->ajax_return(FLAG_OK,'',$topic);
        } else {
            $this->ajax_return(FLAG_ERROR,'暂无内容');
        }
    }

    /**
     * APP首页话题详情
     * @author rkm
     * @param $topic_id
     * @date 2016-3-1
     */
    public function topicDetailed(){
        $action = 'APP首页话题详情';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $topic_id = $this->get_gp('topic_id', 'G');
        $model=m('show');
        $show_id=$model->getShowId($topic_id);
        if(count($show_id)>=2){
            $data=$model->getShowList($show_id);
            if($data){
                $this->ajax_return(FLAG_OK,'',$data);
            }else{
                $this->ajax_return(FLAG_ERROR,'暂无内容');
            }
        }else{
                $this->getShowDetailed($show_id);
        }
    }

    //获取达人秀详情
    public function getShowDetailed($show_id=null){
            $action = '获取达人秀详情';
            setLogParameter($action);       //设置日志的参数
            $method = strtoupper($_SERVER['REQUEST_METHOD']);
            if ($method != 'GET') {
                $this->ajax_return(FLAG_ERROR, '非法操作！');
            }
            if(!$show_id){
                $show_id = $this->get_gp('show_id', 'G');
            }
            if(is_array($show_id)){
                $show_id = $show_id[0]['show_id'];
            }
            $user_id = $this->checkLogin();                   //检验是否登录
            $model=m('show');
            $common = $this->common;
            $data = $model->oneDetail($show_id,$common,$user_id);
            if($data){
                $this->ajax_return(FLAG_OK,'',$data);
            }else{
                $this->ajax_return(FLAG_ERROR,'暂无内容');
            }
    }

    /**
     * 获取十个最火的话题
     * @author rkm
     * @date 2016-3-1
     */
    public function getHotTopic(){
        $action = '获取十个最火的话题';
        setLogParameter($action);       //设置日志的参数
        $model=m('show');
        $topic_id=$model->getTopicId();     //获取话题id
        $topics=$model->getTenTopic($topic_id);     //获取十个最火的话题
        if($topics){
            $this->ajax_return(FLAG_OK,'',$topics);
        }else{
            $this->ajax_return(FLAG_ERROR,'暂无内容',$topics);
        }
    }

    /**
     * 删除达人秀
     * @author rkm
     * @data 2016-4-13
     *
     * */
    public function delShow()
    {
        $action="删除达人秀";
        setLogParameter($action);
        $this->check_method('POST');
        if(!$user_id=$this->checkLogin()){
            $this->ajax_return(FLAG_ERROR, '请您登陆！');
        }
        $show_id=$this->get_gp('show_id','P');
        if(!$show_id){
            $this->ajax_return(FLAG_ERROR, "达人秀不能为空！");
        }
        $model=m(show);
        $data = $model->delShow($show_id,$user_id);
        if($data){
            $this->ajax_return(FLAG_OK,'删除成功');
        }else{
            $this->ajax_return(FLAG_ERROR,'删除失败');
        }

    }

    /**
     * 发布达人秀
     * @author lz
     * @date 2016-1-28
     */
    public function publish(){
        $action = '发布达人秀';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'POST') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->checkLogin();
        if(!$user_id){
            $this->ajax_return(FLAG_ERROR, '请您登陆！');
        }
        $description = $this->get_gp('description', 'P');
        $topic = $this->get_gp('topic','P');
        $goods_id = $this->get_gp('goods_id', 'P');

       /* $user_id = 85;
        $description = "这是描述描述描述";
        $topic = "#圣诞节3#,#春节3#,#情人节3#";
        $goods_id = "1223,2525,2152";*/
        /**描述不能为空**/
        if (!$description) {
            $this->ajax_return(FLAG_ERROR, "描述不能为空！");
        }
        /**话题内容不能为空**/
        if (!$topic) {
            $this->ajax_return(FLAG_ERROR, "话题不能为空！");
        }
        /**关联商品不能为空**/
        if (!$goods_id) {
            $this->ajax_return(FLAG_ERROR, "关联商品不能为空！");
        }
        //上传图片
       $file = $_FILES['file'];
        if ($file['error'] != UPLOAD_ERR_OK) {
            $this->ajax_return(FLAG_ERROR, '上传图片错误！');
        }
        import('uploader.lib');
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE);
        $uploader->addFile($file);
        $model = m('member');
        //七牛云上传图片
        $filename = $uploader->random_filename();
        $filename = 'data/files/topic/images/'.date('Y').'/'.date('m').'/'.date('d').'/'.$filename.$user_id.'.jpg';
        if (!is_dir(dirname($filename))) {
            $res = @mkdir(dirname($filename), 0777, true);
            if ($res === false) {
                $this->ajax_return(FLAG_ERROR, '创建目录失败!');
            }
        }
        $rs = @move_uploaded_file($file['tmp_name'], ROOT_PATH.'/'.$filename);
        if ($rs === false) {
            $this->ajax_return(FLAG_ERROR, '移动图片失败!');
        }

        $model = m('show');
        //发布达人秀
        $show_id = $model->publish($user_id,$description,$filename);
        if($show_id) {
            /** 发布话题 */
            $model->topic($show_id,$topic);

            /** 关联商品 */
            $model->goods($show_id,$goods_id);

            $this->ajax_return(FLAG_OK, '发布成功！');
        } else {
            $this->ajax_return(FLAG_ERROR, '发布失败！');
        }
    }

    /**
     * 添加或取消喜欢达人秀
     * @author lz
     * @date 2016-2-17
     */
    public function favorite(){
        $action = '添加或取消喜欢达人秀';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'POST') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->checkLogin();
        if (!$user_id) {
            $this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
        }
        $show_id = $this->get_gp('show_id', 'P');
        $favorite = $this->get_gp('favorite','P');
        
        if(!$show_id){
            $this->ajax_return(FLAG_ERROR, '该达人秀不存在');
        }
        $model = m('show');
        //判断为添加喜欢或者取消喜欢
        $judge=$model->getTopicIdByShowId($show_id);
        if(empty($judge)){
            $this->ajax_return(FLAG_ERROR, '这个达人秀不存在');
        }
        $data = $model->getFavorite($show_id,$user_id);
        if($favorite){
            //取消喜欢
            if($data==0){
                $this->ajax_return(FLAG_ERROR, '你还没有喜欢该达人秀');
            }
            $res = $model->delFavorite($user_id,$show_id);
            if($res) {
                $this->ajax_return(FLAG_OK, '取消喜欢成功!',array('islike'=>0));
            } else {
                $this->ajax_return(FLAG_ERROR, '取消喜欢失败!');
            }
        } else {
            //添加喜欢
            if($data>=1){
                $this->ajax_return(FLAG_ERROR, '你已经喜欢该达人秀');
            }
            $res = $model->addFavorite($user_id,$show_id);
            if($res) {
                $this->ajax_return(FLAG_OK, '添加喜欢成功!',array('islike'=>1));
            } else {
                $this->ajax_return(FLAG_ERROR, '添加喜欢失败!');
            }
        }
    }

    /**
     * 获取达人秀评论
     * @author lz
     * @date 2016-2-17
     */
    public function comment(){
        $action = '获取达人秀评论';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $show_id = $this->get_gp('show_id', 'G');
        if(!$show_id) {
            $this->ajax_return(FLAG_ERROR, '无效的参数！');
        }
        $model = m('show');
        $comment = $model->getComments($show_id);
        if($comment) {
            $this->ajax_return(FLAG_OK,'',$comment);
        } else {
            $this->ajax_return(FLAG_ERROR, '暂无评论！');
        }
    }

    /**
     * 添加达人秀评论
     * @author lz
     * @date 2016-2-17
     */
    public function addComment(){
        $action = '添加达人秀评论';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'POST') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->checkLogin();
        if (!$user_id) {
            $this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
        }
        $show_id = $this->get_gp('show_id', 'P');
        $content = $this->get_gp('content', 'P');
        /**用户名不能为空**/
        if (!$content) {
            $this->ajax_return(FLAG_ERROR, "评论不能为空！");
        }
        /**show_id不能为0**/
        if (!$show_id) {
            $this->ajax_return(FLAG_ERROR, "参数传递错误");
        }
        $model = m('show');
        $res = $model->addComments($user_id,$show_id,$content);
        if($res) {
            $this->ajax_return(FLAG_OK, '添加评论成功！');
        } else {
            $this->ajax_return(FLAG_ERROR, '添加评论失败！');
        }
    }

    /**
     * 测试接口--没用
     */
    public function token(){
        /*$host = '10.66.123.5';
        $instance = 'crs-77hndx96';
        $pass = 'Rnf1qW3H';
        $redis = new RedisDB($host, $instance.":".$pass);
        $c = $redis->get('3mduqz1455791811');
        echo $c;*/

        import('redis.lib');
        $redis = new RedisDB(REDISHOST,REDISPWD);
        //存储Redis $token作为key $user_id作为value
        //$value = $sign."-".$user_id;
        $idfa = "0vwjdt1455792531";
        $value = "0vwjdt1455792531-test_sign_56c5a1934bca0565617421";
        //$result = $redis->setex($idfa,$value,7200);
        //var_dump($result);
        $token = "je9wu81455793672";
        $result = $redis->get($token);
        $time = $redis->ttl($token);
        echo "剩余".$time."秒";
        var_dump($result);
    }

    /**
     * 返回我的达人秀
     * @author lz
     * @date 2016-2-22
     */
    public function daRen(){
        $action = '返回我的达人秀';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->get_gp('user_id', 'G');
        if (!$user_id) {
            $this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
        }
        $page = $this->get_gp('page', 'G');
        $size = $this->get_gp('size', 'G');
        $page = $page ? intval($page) : 1;
        $size = $size ? intval($size) : 10;
        $offset = ($page > 1) ? ($page - 1) * $size : 0;
        $model = m('show');
        $show = $model->getMyDarenShow($user_id,$offset,$size);
        if($show['total']) {
            $this->ajax_return(FLAG_OK,'',$show);
        } else {
            $this->ajax_return(FLAG_ERROR,'暂无数据');
        }
    }

    /**
     * 我的达人秀话题
     * @author lz
     * @date 2016-2-22
     */
    public function myTopic($user_id=null){
        $action = '我的达人秀话题';
        setLogParameter($action);       //设置日志的参数
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->get_gp('user_id', 'G');
        $page = $this->get_gp('page', 'G');
        $size = $this->get_gp('size', 'G');
        $page = $page ? intval($page) : 1;
        $size = $size ? intval($size) : 100;
        $offset = ($page > 1) ? ($page - 1) * $size : 0;
        //获取达人秀列表
        $model = m('show');
        $topic = $model->getMyTopicList($offset,$size,$user_id);
        if($topic){
            $this->ajax_return(FLAG_OK,'',$topic);
        } else {
            $this->ajax_return(FLAG_ERROR,'暂无内容');
        }
    }






















}